/*  David Neimeyer 2007 |  www.eathaiku.com
 * -----------------------------------------------------------
 *
 * Mootools events for bookstore component (for use within Joomla 1.5)
 *
 */

window.addEvent('domready', function(){
		var items = $$('.ann');
		items.each(function(item){
		
			item.addEvent('click', function(e) {
				var isbn = item.getAttribute('name');
				e = new Event(e).stop();		
				//alert(isbn.substr(0,6));		
				if(isbn.substr( 0,6) != 'detail'){
					var item_div = $('ann'+isbn);			
					var fx = item_div.effects({wait:true, duration: 1200, transition: Fx.Transitions.Circ.easeInOut});
					
					//define image
					var box = $('box'+isbn);
					var fxbox =  box.effects({duration: 1000,wait:false, transition: Fx.Transitions.Circ.easeInOut});
					//var thumb = $('image'+isbn);
					var thumb = document.createElement('img');
					thumb.setAttribute('id', 'img' + isbn);
					//var thumb = $('img'+isbn);
					
					//alert(thumb);
					//var fx2 = thumb.effects({duration: 800,wait:true, transition: Fx.Transitions.Circ.easeInOut});
					
					//define p
					var p = document.createElement('p');
					p.setAttribute('id', 'p_ann' + isbn);
					
				
					function pausecomp(millis){
						var date = new Date();
						var d= date.getTime();
						var curDate = null;
						
						do { curDate = new Date(); var c=curDate.getTime(); }
						while(c-d < millis);
					}
					
					function p_height(obj){
						//var len = obj.textContent.length;
						var len = p.innerHTML.length;
						//alert(len);
						if( len > 280){
							var remainder = len % 4;
							var height = ( (len - remainder)/4) ;
							return height;
						}else if( len < 279 && len > 60){
							var height = '75';
							return height;
						}else if(len < 59 && len > 30){
							var height = len;
							return height;
						}else if(len < 29 && len > 0){
							var height = 30;
							return height;
						}else{
							//alert(p.innerHTML.length);
							//var height = '68';
							var height = 0;
							return height;
						}
					}
					
					function t_height(obj){ //same as p_height but will use textContent
						//var len = obj.textContent.length;
						var len = p.textContent.length;
						//alert(len);
						if( len > 240){
							var remainder = len % 4;
							var height = ( (len - remainder)/4) ;
							return height;
						}else if( len < 239 && len > 20){
							var height = '75';
							return height;
						}else if(len < 19 && len > 0){
							var height = len;
							return height;
						}else{
							//alert(p.innerHTML.length);
							//var height = '68';
							var height = 0;
							return height;
						}
					}
					//fx2.start({'width': '0px', 'height': '0px','opacity': 0});
				// uncomment for isbn cover
					/*
					fxbox.start({'width': '100px', 'height': '150px', 'opacity': '1'})
						.chain( function(){ 
							thumb.setAttribute('width',100);
							thumb.setAttribute('height',150);
							box.appendChild(thumb);
							//this.start({'opacity':'1','width': '100px','height': '150px'});
						});
					*/
					fx.start({'opacity' : 0}).
						chain(function(){ 
						///////////////////////////////////
						
							var url = "/stable/components/com_bookstore/detail.php?isbn=" + isbn;
							var jSonRequest = new Json.Remote(url, {onComplete: function(details){
								//alert(details.annotation);
								var review = document.getElementById('rev_div'+isbn);
								if(review){
									var det_ann = document.createElement('div');
									det_ann.innerHTML = details.annotation;
								
									var rev_ann = review.cloneNode(true);
									rev_ann.style.display = 'block';
									
									p.appendChild(rev_ann);
									p.appendChild(det_ann);
								
									var h = p_height(p);
								
									fx.start({ 'width': '580px',
													'height': h+'px', 
													'zindex': '1000'
									}).
									chain(function(){
										  this.start({'opacity' : 1});
									 });									
								}else{
									p.innerHTML = details.annotation;
									var h = p_height(p);
									var pars = new Array();
									var breaks = new Array();
									if( breaks = p.innerHTML.match(/.<br./g) ){
										//alert(breaks.length+' breaks');
										if(breaks.length > 1){
											var x;
											var i=1;
											for (x in breaks){
												if(i <9){
													h=h+10-i;
												}else{
													h=h+2;
												}
												i++;
											}
										}
									}
									if( pars = p.innerHTML.match(/.<p./g) ){
										//alert(pars.length+' pars');
										if(pars.length > 1){
											var x;
											var i=1;
											for (x in pars){
												if(i <11){
													h=h+12-i;	
												}else{
													h=h+2;
												}
												i++;
											}
											
										}
									}
									
									fx.start({ 'width': '560px',
													'height': h+'px',
													'margin': '5px 0px 5px 0px',
													'zindex': '1000'
									}).
									chain(function(){
										  this.start({'opacity' : 1});
									 });
								}
								
								//p.appendChild( document.createTextNode( details.annotation ) ); 
								// define thumb
								//thumb.setAttribute('src',details.graphics);
							}}).send();
										   
						//////////////////////////////////		   
							
							p.setAttribute('class','p_ann');
							
							item_div.insertBefore(p, item.firstChild.nextSibling);
							item.setHTML('Hide Details');
							item.setAttribute('class','xann');
							item.setAttribute('name','detail' + isbn);
							
						});//end chain/start
						
					}else{ //detail is already there
						//alert(isbn.substr(6));
						var box = $('box'+isbn.substr(6));
						var fxbox =  box.effects({duration: 800,wait:false, transition: Fx.Transitions.Circ.easeInOut});
					// remove thumb
					//	var thumb = $('img' + isbn.substr(6));
					//	fxbox.start({'opacity':0}).chain(function(){ box.removeChild(thumb); this.start({'width': '0px','height': '0px'});});
						var item_div = $('ann'+isbn.substr(6));	
						var p = $('p_ann' + isbn.substr(6));			
						var fx = item_div.effects({duration: 1000, transition: Fx.Transitions.Circ.easeInOut});
						fx.start({'opacity' : '0'}).
						chain(function(){ 
							var html = 'More Book Details';
							item.setHTML(html);
							item.setAttribute('class','ann');
							item.setAttribute('name',isbn.substr(6));
							item_div.removeChild(p);
							//p.removeChild(p.childNodes);
							this.start({
											'width': '100px',
											'height': '20px',
											'margin': '15px 0px 0px 0px',
											'zindex': '1'
										});//end thisstart
						}).
						chain(function(){
							this.start({'opacity' : 1});
						});//end chain/start
					}//end if detailisbn
			});// event
		});//end each
		///////////////////////////////////// 
		/////////////////////////////////////
		
}); //end window event